package com.xhj.myframe.myspring.aop.aspectJ;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
/**
 * -
 *
 * @author 见见 pure_xhj@163.com @Date 2021-08-22 @Time 13:53
 */
@RestController
@RequestMapping("aspectJ")
public class Test {
  @Autowired Calculation calculation;
  @Autowired MyCatch myCatch;
  AtomicInteger order = new AtomicInteger(0);

  @RequestMapping("test")
  public String test() {
    Random random = new Random();
    int nextInt = random.nextInt(1000000);
    int sum = 0;
    order.getAndIncrement();
    if (order.get() == 1) {
      sum = calculation.sum(String.valueOf(order), 12, 13);
    } else {
      order.getAndIncrement();
      sum = calculation.sum(String.valueOf(nextInt), 12, 13);
    }
    System.out.println("sum = " + sum);
    return String.valueOf(order.get());
  }

  @RequestMapping("cleanCatch")
  public String cleanCatch() {
    System.out.println("start clean: ");

    System.out.println("myCatch.num = " + myCatch.num + " , myCatch.i = " + myCatch.i);
    System.out.println(Thread.currentThread().getName() + ": myCatch.flag = " + myCatch.flag);
    System.out.println("order = " + order.get());
    System.out.println("after clean: ");

    myCatch.flag = 0;
    myCatch.i = 0;
    myCatch.num.getAndSet(0);
    System.out.println("myCatch.num = " + myCatch.num + " , myCatch.i = " + myCatch.i);
    System.out.println(Thread.currentThread().getName() + ": myCatch.flag = " + myCatch.flag);
    System.out.println("order = " + order.get());
    return "cleanCatch done";
  }
}
